package com.twq.oo.abstractscls.forth

/**
 * Created by tangweiqun on 2017/11/28.
 *
 */
abstract class Element {
  def contents: Array[String]

  def width: Int = contents(0).length

  def height: Int =
    if (height == 0) 0 else contents(0).length

  //1、实现方法above、beside、widen、heighten以及toString
  //将this Element放置在that Element之上
  def above(that: Element): Element = {
    val this1 = this widen that.width
    val that1 = that widen this.width
    new ArrayElement(this1.contents ++ that1.contents)
  }

  //将this Element放置在that Element旁边
  def beside(that: Element): Element = {
    val this1 = this heighten that.height
    val that1 = that heighten this.height
    val contents = new Array[String](this1.contents.length)
    for (i <- 0 until this1.contents.length)
      contents(i) = this1.contents(i) + that1.contents(i)
    new ArrayElement(contents)
  }

  //将当前的Element进行加宽处理(加宽到w)
  def widen(w: Int): Element =
    if (w <= width) this
    else {
      val left = new UniformElement(' ', (w - width) / 2, height)
      val right = new UniformElement(' ', w - width - left.width, height)
      left beside this beside right
    }

  //将当前Element进行加高处理(加高到h)
  def heighten(h: Int): Element =
    if (h <= height) this
    else {
      val top = new UniformElement(' ', width, (h - height) / 2)
      val bot = new UniformElement(' ', width, h - height - top.height)
      top above this above bot
    }

  override def toString = contents mkString "\n"

}

class ArrayElement(
                    val contents: Array[String]
                  ) extends Element


class LineElement(s: String) extends Element {
  val contents = Array(s)

  override def width = s.length

  override def height = 1
}

class UniformElement(
                      ch: Char,
                      override val width: Int,
                      override val height: Int
                    ) extends Element {
  private val line = ch.toString * width

  def contents = Array.fill(height)(line)
}

object Spiral {
  def main(args: Array[String]): Unit = {

  }
}